package cn.com.captureNetPacket.marge;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;

import cn.com.captureNetPacket.CaptureNetPacketService;
import cn.com.captureNetPacket.model.ConfigData;
import cn.com.captureNetPacket.record.WavFileInfo;
import cn.com.captureNetPacket.util.WavFileUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class MargeThread {
	ConfigData configData=CaptureNetPacketService.getAppConfig();
		public   void margeStart() {
			try {
//			testMageFile();
			testMergeWave();
//			testcheckWaveFile();
			
			
			
			}catch (Exception e) {
				log.error("Exception.....................",e);
			}
		}
		
		public   void testcheckWaveFile() {
			try {
				WavFileUtil.getWaveFileInfo(configData.getTempFilePath()+File.separator+"1.V3");
			} catch (JsonProcessingException e) {
				log.error("JsonProcessingException.....................",e);
			} catch (FileNotFoundException e) {
				log.error("FileNotFoundException.....................",e);
			}
		}
		// 测试文件合并功能 testMergeWave。A law，8000,8bit文件测试合成没有问题。 2个文件合成到一个文件的两个声道里面。
		public   void testMergeWave() {
			log.info("margeStart.....................");
			testSoundApiFile margeFile1,margeFile2,margeOutFile;
			try {
				byte[] fact_data= ByteBuffer.allocate(4).put("fact".getBytes("ISO_8859_1")).array();
				 WavFileInfo wavFileInfo=new WavFileInfo((short)6, (short)2, (int)8000, (short)8, 0,(short)12,fact_data);
					WavFileUtil.mergeWave(wavFileInfo, configData.getTempFilePath()+File.separator+"1.V3",configData.getTempFilePath()+File.separator+"2.V3", configData.getTempFilePath()+File.separator+"out.V3");
			} catch (UnsupportedEncodingException e) {
				log.error("margeStart.....................",e);
			}catch (Exception e) {
				log.error("margeStart.....................",e);
			}
		}
	 
		//  
		public   void testMixer() {
			log.info("margeStart.....................");
			testSoundApiFile margeFile1,margeFile2,margeOutFile;
			try {
				testSoundApiFile.listMixers();
				testSoundApiFile.listSupportedTypes();
				margeFile1 = new testSoundApiFile(configData.getTempFilePath()+"/1.V3");
				margeFile1.getFileInfo();
				margeFile2 = new testSoundApiFile(configData.getTempFilePath()+"/2.V3");
				margeFile2.getFileInfo();
				
				Mixer mixer =AudioSystem.getMixer(testSoundApiFile.getMixerInfo(3));
				
				margeOutFile= new testSoundApiFile(configData.getTempFilePath()+"/out.V3");
				margeOutFile.getFileInfo();
			} catch (FileNotFoundException e) {
				log.error("margeStart.....................",e);
			} catch (Exception e) {
				log.error("margeStart.....................",e);
			}
		}
}
